package com.lkk.cg.action;

import com.lkk.cg.base.BaseAction;
import com.lkk.cg.domain.Article;
import com.lkk.cg.domain.User;
import com.lkk.cg.util.QueryHelper;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 * Created by lkk on 2014/12/8.
 */
@Controller
@Scope("prototype")
public class UserAction extends BaseAction<User> {

    //todo 添加校验
    private String confirmPassword;

    /**
     * 登陆页面
     *
     * @return
     */
    public String loginUI() {
        return "loginUI";
    }

    /**
     * 登陆
     *
     * @return
     */
    @InputConfig(resultName = "loginUI")
    public String login() {

        User user = userService.getByLoginName(model.getLoginName());
        if (null == user) {
            addActionError("用户名不存在！");
            return "loginUI";

        } else if (!(user.getPassword().equals(DigestUtils.md5Hex(model.getPassword())))) {
            addActionError("密码错误！");
            return "loginUI";
        } else {
            ActionContext.getContext().getSession().put("user", user);
            return "toIndex";
        }

    }

    /**
     * 登注销
     *
     * @return
     */
    public String logout() {

        ActionContext.getContext().getSession().remove("user");

        return "toIndex";
    }

    /**
     * 注册页面
     *
     * @return
     */
    public String registerUI() {
        return "registerUI";
    }

    /**
     * 登陆
     *
     * @retu
     */
    @InputConfig(resultName = "registerUI")
    public String register() {

        addActionMessage("注册成功，请登录！");
        model.setRole(roleService.getDefaultRole());
        model.setPassword(DigestUtils.md5Hex(model.getPassword()));
        userService.save(model);

        log.debug("user register success");
        return "toLogin";
    }

    public void validateRegister() {
        User user = userService.getByLoginName(model.getLoginName());
        if (user != null)
            addActionError("用户名已经存在！");
    }

    /**
     * 文章列表
     *
     * @return
     */
    public String articleList() {

        QueryHelper queryHelper = new QueryHelper(Article.class, "a");
        queryHelper.addCondition("a.author.id=?", getCurrentUser().getId());
        queryHelper.preparePageBean(articleService, getPageNum(), getPageSize());

        return "articleList";
    }

    /**
     * 删除文章
     *
     * @return
     */
    public String articleDelete() {

        Article article = articleService.getById(model.getId());
        if (article != null && article.getAuthor() != getCurrentUser()) {
            addActionError("该文章不存在！");
        } else {
            articleService.delete(model.getId());
            addActionMessage("文章删除成功");
        }

        return "toArticleList";
    }

    /**
     * 信息修改页面
     *
     * @return
     */
    public String editProfileUI() {
        ActionContext.getContext().getValueStack().push(getCurrentUser());
        return "editProfileUI";
    }

    /**
     * 信息修改
     *
     * @return
     */
    @InputConfig(resultName = "editProfileUI")
    public String editProfile() {
        User user = getCurrentUser();

        user.setName(model.getName());
        user.setDepartment(model.getDepartment());
        user.setEmail(model.getEmail());
        if (model.getPassword() != null)
            user.setPassword(DigestUtils.md5Hex(model.getPassword()));

        log.debug("update user id=" + user.getId());
        userService.update(user);

        addActionMessage("信息修改完成！");

        return "toEditProfileUI";
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

}
